package com.woniu.inlet.web;

import com.woniu.adapter.LoggingOpenFeign;
import com.woniu.adapter.MatApplicationOpenFein;
import com.woniu.command.Command;
import com.woniu.command.CommandInteger;
import com.woniu.command.CommandString;
import com.woniu.command.inventoryCommand.ProjectInCommand;
import com.woniu.command.inventoryCommand.ProjectOutCommand;
import com.woniu.command.inventoryCommand.PurchaseDetailCommand;
import com.woniu.command.outApplyCommand.AddMaterialOutApplyCommand;
import com.woniu.command.outApplyCommand.UpdateMaterialOutApplyCommand;
import com.woniu.command.productCommand.GetProjectCodeCommand;
import com.woniu.command.purchaseCommand.UpdatePurchaseCommand;
import com.woniu.inlet.vo.InventoryInoutVo;
import com.woniu.inlet.vo.InventoryVo;
import com.woniu.inlet.vo.ResponseResult;
import com.woniu.outlet.po.*;
import com.woniu.service.InventoryInoutPoService;
import com.woniu.service.InventoryPoService;
import com.woniu.service.MaterialOutApplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
@Api(tags = "仓库controller")
@RestController
@Transactional
public class WavehouseController {


    @Autowired
    private InventoryInoutPoService inventoryInoutPoService;

    @Autowired
    private InventoryPoService inventoryPoService;

    @Autowired
    private MaterialOutApplyService materialOutApplyService;

    @Autowired
    @Qualifier("provider")
    private MatApplicationOpenFein matApplicationOpenFein;

    @Autowired
    @Qualifier("log")
    private LoggingOpenFeign loggingOpenFeign;

    /**
     * 添加退料单
     * @param materialOutApplyPo
     */
    @ApiOperation("原料退料申请/添加退料单")

    @PostMapping("/addOutApply")
    @ResponseBody
    public ResponseResult<String> addMaterialOutApply(@RequestBody MaterialOutApplyPo materialOutApplyPo){
        Command command = new AddMaterialOutApplyCommand();
        command.execute(materialOutApplyPo);
        loggingOpenFeign.addSystemLog("原料退料申请/添加退料单");
        return new ResponseResult<String>(200,"添加成功");
    }

    /**
     * 完成退料单
     * @param id 采购单id
     */
    @ApiOperation("原料退料申请/完成退料单")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",value = "购单id",paramType = "query",
                    dataType = "int"  ,example ="1"),
    })
    @PostMapping("/updateOutApply")
    @ResponseBody
    public ResponseResult<String> updateMaterialOutApply(Integer id){
        Command command = new UpdateMaterialOutApplyCommand();
        command.execute(id);
        loggingOpenFeign.addSystemLog("原料退料申请/完成退料单");
        return new ResponseResult<String>(200,"修改成功");
    }

    /**
     * 物料入库查询
     * 根据采购单id查询采购详细表
     * @return
     */
    @ApiOperation("物料入库查询/采购物料入库查询")
    @GetMapping("/findInventoryInByPurchase")
    @ResponseBody
    public ResponseResult<List<InventoryInoutVo>> findInventoryInByPurchase(){
        List<InventoryInoutPo> inventoryInoutPos= inventoryInoutPoService.findInventoryInByPurchase();
        List<InventoryInoutVo> inventoryInoutVos = new ArrayList<>();
        for (InventoryInoutPo inventoryInoutPo : inventoryInoutPos) {
            InventoryInoutVo inventoryInoutVo = new InventoryInoutVo();
            BeanUtils.copyProperties(inventoryInoutPo,inventoryInoutVo);
            inventoryInoutVos.add(inventoryInoutVo);
        }
        loggingOpenFeign.addSystemLog("物料入库查询/采购物料入库查询");
        return new ResponseResult<List<InventoryInoutVo>>(inventoryInoutVos,"查询成功",200);
    }

    /**
     * 原料出库查询
     * @return
     */
    @ApiOperation("原料出库查询")
    @GetMapping("/findInventoryOutByPurchase")
    @ResponseBody
    public ResponseResult<List<InventoryInoutVo>> findInventoryOutByPurchase(){
        List<InventoryInoutPo> inventoryInoutPos=inventoryInoutPoService.findInventoryOutByPurchase();
        List<InventoryInoutVo> inventoryInoutVos = new ArrayList<>();
        for (InventoryInoutPo inventoryInoutPo : inventoryInoutPos) {
            InventoryInoutVo inventoryInoutVo = new InventoryInoutVo();
            BeanUtils.copyProperties(inventoryInoutPo,inventoryInoutVo);
            inventoryInoutVos.add(inventoryInoutVo);
        }
        loggingOpenFeign.addSystemLog("原料出库查询");
        return new ResponseResult<List<InventoryInoutVo>>(inventoryInoutVos, "查询成功",200);
    }

    /**
     * 查询库存详情
     * @return
     */
    @ApiOperation("库存查询")
    @GetMapping("/findInventorys")
    @ResponseBody
    public ResponseResult<List<InventoryVo>> findInventorys(){
        List<InventoryPo> inventoryPos=inventoryPoService.findInventorys();
        List<InventoryVo> inventoryVos = new ArrayList<>();
        for (InventoryPo inventoryPo : inventoryPos) {
            InventoryVo inventoryVo = new InventoryVo();
            BeanUtils.copyProperties(inventoryPo,inventoryVo);
            inventoryVos.add(inventoryVo);
        }
        loggingOpenFeign.addSystemLog("库存查询");
        return new ResponseResult<List<InventoryVo>>(inventoryVos,"查询成功",200);
    }

    /**
     *查询退料单列表
     * @return
     */
    @ApiOperation("退料出库管理/查询退料单列表")
    @PostMapping("/findMaterialOutApplyPos")
    @ResponseBody
    public ResponseResult<List<MaterialOutApplyPo>> findMaterialOutApplyPos(){
        List<MaterialOutApplyPo> materialOutApplyPos = materialOutApplyService.findMaterialOutApplyPos();
        loggingOpenFeign.addSystemLog("退料出库管理/查询退料单列表");
        return new ResponseResult<List<MaterialOutApplyPo>>(materialOutApplyPos,"查询成功",200);
    }

    /**
     *查询退料详情
     * @param id 退料申请单id
     * @return
     */
    @ApiOperation("退料出库管理/查询退料详情")
    @GetMapping("findMaterialOutDetials")
    @ResponseBody
    public ResponseResult<List<InventoryInoutVo>> findMaterialOutDetials(Integer id){
        List<InventoryInoutPo> inventoryInoutPos=
                inventoryInoutPoService.findMaterialOutDetialsByOutApplyId(id);
        List<InventoryInoutVo> inventoryInoutVos=new ArrayList<>();
        for (InventoryInoutPo inventoryInoutPo : inventoryInoutPos) {
            InventoryInoutVo inventoryInoutVo = new InventoryInoutVo();
            BeanUtils.copyProperties(inventoryInoutPo,inventoryInoutVo);
            inventoryInoutVos.add(inventoryInoutVo);
        }
        loggingOpenFeign.addSystemLog("退料出库管理/查询退料详情");
        return new ResponseResult<List<InventoryInoutVo>>(inventoryInoutVos, "查询成功",200);
    }


    /**
     *修改用料请领单的状态
     * @param applicationCode 用料请领单的编码
     */
    @PostMapping("/receive")
    @ApiOperation("用料请领出库/修改用料请领单的状态")
    @ResponseBody
    public ResponseResult<String> receive( String applicationCode){
        matApplicationOpenFein.receive(applicationCode);
        loggingOpenFeign.addSystemLog("用料请领出库/修改用料请领单的状态");
        return new ResponseResult<String>(200,"请领单已领取");
    }

    /**
     *修改用料请领单的状态
     * @param  id
     */
    @PostMapping("/updateStatus")
    @ApiOperation("根据成品入库单的id/成品入库")
    @ResponseBody
    public ResponseResult<String> updateStatus(Integer id){
        matApplicationOpenFein.updateStatus(id);
        Command command = new ProjectInCommand();
        command.execute(id);
        loggingOpenFeign.addSystemLog("根据成品入库单的id/成品入库");
        return new ResponseResult<String>(200,"入库成功");
    }

    /**
     * 根据采购单对物料进行入库
     * @param id 采购单详细code
     * @param num 不达标的原料数
     */
    @PostMapping("/finishPurchase")
    @ApiOperation("原料采购入库")
    @ResponseBody
    public ResponseResult<String> finishPurchase(Long id,Integer num){
        InventoryPo inventoryPo = new InventoryPo();
        inventoryPo.setCode(id.toString());
        inventoryPo.setNum(num);
        CommandString command = new PurchaseDetailCommand();
        String a=command.execute(inventoryPo);
        loggingOpenFeign.addSystemLog("原料采购入库");
        return new ResponseResult<String>(200,a);
    }


    @PostMapping("/updatePurchaseStatus")
    @ApiOperation("原料采购验收")
    @ResponseBody
    public ResponseEntity<String> updatePurchaseStatus(Long code){
        CommandString command = new UpdatePurchaseCommand();
        String execute = command.execute(code);
        loggingOpenFeign.addSystemLog("原料采购验收");
        return new ResponseEntity<String>(execute,HttpStatus.OK);
    }

    /**
     * 产品出库
     * @param code 产品code
     * @param num 订单上需要的产品数量
     * @return 返回的是减库存的产品数量
     */
    @PostMapping("/projectOut")
    @ApiOperation("产品出库")
    @ResponseBody
    public ResponseEntity<Integer> projectOut(String code,Integer num){
        InventoryPo inventoryPo = new InventoryPo();
        inventoryPo.setNum(num);
        inventoryPo.setCode(code);
        CommandInteger command = new ProjectOutCommand();
        Integer execute = command.execute(inventoryPo);
        loggingOpenFeign.addSystemLog("产品出库");
        return new ResponseEntity<Integer>(execute,HttpStatus.OK);
    }

    /**
     * 根据产品id查询产品code
     * @return
     */
    @GetMapping("/getProjectCodeById")
    @ResponseBody
    @ApiOperation("根据产品id查询产品code")
    public ResponseEntity<String> getProjectCodeById(Integer id){
        CommandString command = new GetProjectCodeCommand();
        loggingOpenFeign.addSystemLog("根据产品id查询产品code");
        return new ResponseEntity<String>(command.execute(id),HttpStatus.OK);
    }
}
